package testHutools.testJsonUtil;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.junit.Test;

import java.util.List;

/**
 * @version 1.0.0
 * Created by cyd on 2022-09-09 17:40:48
 * @ClassName : hutools.json.TestJson
 * @Description : 类描述
 */
public class TestJson {

    @Test
    public void test() {
        String data="[100,200,300,400,500,600,700,800,1500,1600,9001,9002,8001]";
        List<String> list = JSONUtil.toList(data, String.class);
        boolean result = list.contains("100");
        System.out.println(result);
    }

    /**
     * 返回值
     */
    static String result = "{\n" +
            "    \"uuid\":\"d949b9fda7e046fb9cb895cfcd3fc856\",\n" +
            "    \"code\":\"0\",\n" +
            "    \"message\":\"OK\",\n" +
            "    \"data\":{\n" +
            "        \"pageSize\":10,\n" +
            "        \"pageIndex\":1,\n" +
            "        \"total\":449468,\n" +
            "        \"pages\":44947,\n" +
            "        \"beginIndex\":0,\n" +
            "        \"endIndex\":0,\n" +
            "        \"list\":[\n" +
            "            {\n" +
            "                \"alarmContent\":\"智慧井盖发生告警，请及时处理\",\n" +
            "                \"address\":\"广东省深圳市龙岗区龙德南路\",\n" +
            "                \"lng\":\"114.24517\",\n" +
            "                \"streetname\":\"宝龙街道\",\n" +
            "                \"alarmTime\":1661495984000,\n" +
            "                \"communityname\":\"宝龙社区\",\n" +
            "                \"alarmName\":\"智慧井盖-A5告警\",\n" +
            "                \"deviceId\":\"SZ0102200126\",\n" +
            "                \"deviceName\":\"文化绘制井盖_8082619\",\n" +
            "                \"deviceSn\":\"868131061193268\",\n" +
            "                \"productName\":\"文化绘制井盖\",\n" +
            "                \"deviceOneId\":\"440307016001020200078082619\",\n" +
            "                \"alarmDesc\":\"设备上报数据，指令名称为A5，产生报警\",\n" +
            "                \"alarmCount\":4,\n" +
            "                \"alarmId\":6000700,\n" +
            "                \"alarmLevel\":\"普通\",\n" +
            "                \"id\":126647421,\n" +
            "                \"workOrderId\":\"\",\n" +
            "                \"deviceInfoId\":8082619,\n" +
            "                \"workstatus\":\"\",\n" +
            "                \"productInfoId\":6000101,\n" +
            "                \"lat\":\"22.71977\"\n" +
            "            }\n" +
            "        ]\n" +
            "    }\n" +
            "}";

    public static void main(String[] args) {
        // 获取返回值
        boolean isJson = JSONUtil.isJsonObj(result);
        System.out.println(isJson);

        // 转化成jsonObj
        JSONObject jsonObject = JSONUtil.parseObj(result);

        // 获取字段值,根据接口文档，需要哪个就用哪个
        Object uuid = jsonObject.get("uuid");
        System.out.println(uuid.toString());

        // 获取list
        JSONObject data = (JSONObject) jsonObject.get("data");
        JSONArray list = (JSONArray) data.get("list");

        // 获取entity
        JSONObject entity = (JSONObject) list.get(0);
        System.out.println(entity.toString());

        // 获取字段,根据接口文档，需要哪个就用哪个
        String communityname = (String) entity.get("communityname");
        System.out.println(communityname);

        Integer productInfoId = (Integer) entity.get("productInfoId");
        System.out.println(productInfoId);
    }
}
